<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%
String usernameR = request.getParameter("username");
String passwordR = request.getParameter("password");
String repassword = request.getParameter("repassword");
String fname = request.getParameter("fname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String department = request.getParameter("department");
String degree = request.getParameter("degree");

String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
try
{
	Class.forName(driver).newInstance();
	Connection connection = DriverManager.getConnection(url, username, password);
	Statement statement0 = connection.createStatement();
	String sql = "select * from professor where first_name = '"+fname+"' and last_name = '"+lastname+"'";
	ResultSet r = statement0.executeQuery(sql);
	if(!r.next())
	{
		Statement statement = connection.createStatement();
		String query = "insert into professor (first_name,last_name,degree,department,email) values('"+fname+"','"+lastname+"','"+degree+"','"+department+"','"+email+"')";
	
		String query1 = "select id from professor where first_name='"+ fname + "' and last_name='" + lastname +"'";
	
		statement.executeUpdate(query);
		statement = connection.createStatement();
		ResultSet rs = statement.executeQuery(query1);
		if(rs.next())
		{
			String idrs = rs.getString(1);
			String query2 = "insert into user_log_in (username,password,type,ref_id) values('"+usernameR+"','"+passwordR+"','professor',"+idrs+")";
			statement.executeUpdate(query2);
		}
	
		connection.close();
		response.sendRedirect("index.jsp");
	} else
		response.sendRedirect("professorForm.jsp?error=found");
		
}catch(ClassNotFoundException cnfe)
{
		out.println("<br>Couldn't find class file" + cnfe); 
}catch(SQLException sqle)
{
	//out.println("<br>Couldn't find class file" + sqle); 
	response.sendRedirect("professorForm.jsp?error=yes");
}catch(InstantiationException ie)
{
		out.println("<br>Instant exception : " + ie);
}catch(IllegalAccessException iae)
{
		out.println("<br>Illegal access exception : " + iae);
		
}
%>